package com.haixiaoke.saas.enterprise.house.controller;

import com.haixiaoke.saas.common.Node;
import com.haixiaoke.saas.common.annotation.Log;
import com.haixiaoke.saas.common.annotation.RepeatSubmit;
import com.haixiaoke.saas.common.annotation.TenantScope;
import com.haixiaoke.saas.common.core.domain.AjaxResult;
import com.haixiaoke.saas.common.core.domain.entity.SysUser;
import com.haixiaoke.saas.common.core.page.TableDataInfo;
import com.haixiaoke.saas.common.enums.BusinessType;
import com.haixiaoke.saas.common.tenant.TenantContext;
import com.haixiaoke.saas.common.utils.poi.ExcelUtil;
import com.haixiaoke.saas.house.domain.Building;
import com.haixiaoke.saas.house.service.IBuildingService;
import com.haixiaoke.saas.system.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.List;

/**
 * 楼栋信息Controller
 *
 * @author xiaolong
 * @date 2023-10-16
 */
@RestController
@RequestMapping("/enterprise/house/building")
public class EnterpriseBuildingController extends BaseController {
    @Autowired
    private IBuildingService buildingService;

    /**
     * 查询楼栋信息列表
     */
    @PreAuthorize("@ss.hasPermi('house:building:list')")
    @GetMapping("/list")
    @TenantScope
    public TableDataInfo list(Building building) {
        startPage();
        building.setTenantId(TenantContext.getTenantId());
        List<Building> list = buildingService.selectBuildingListInfo(building);
        return getDataTable(list);
    }

    @GetMapping("/level/list")
    @TenantScope
    public List<Node> levelList(Building building) {
        building.setTenantId(TenantContext.getTenantId());
        List<Node> list = buildingService.selectBuildingLevelList(building);
        return list;
    }

    /**
     * 导出楼栋信息列表
     */
    @PreAuthorize("@ss.hasPermi('house:building:export')")
    @PostMapping("/export")
    @TenantScope
    @RepeatSubmit
    public void export(HttpServletResponse response, Building building) {
        building.setTenantId(TenantContext.getTenantId());
        List<Building> list = buildingService.selectBuildingList(building);
        ExcelUtil<Building> util = new ExcelUtil<Building>(Building.class);
        util.hideColumn("estateId","provinceId","cityId","districtId","streetId","communityId","propertyId");
        util.exportExcel(response, list, "楼栋信息数据");
    }

    /**
     * 获取楼栋信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('house:building:query')")
    @GetMapping(value = "/{buildingId}")
    public AjaxResult getInfo(@PathVariable("buildingId") String buildingId) {
        return success(buildingService.selectBuildingByBuildingId(buildingId));
    }

    /**
     * 新增楼栋信息
     */
    @PreAuthorize("@ss.hasPermi('house:building:add')")
    @PostMapping
    @TenantScope
    @RepeatSubmit
    public AjaxResult add(@RequestBody @Valid Building building) {
        Long userId = getUserId();
        building.setTenantId(TenantContext.getTenantId());
        return toAjax(buildingService.insertBuilding(building,userId));
    }

    /**
     * 修改楼栋信息
     */
    @PreAuthorize("@ss.hasPermi('house:building:edit')")
    @PutMapping
    @TenantScope
    @RepeatSubmit
    public AjaxResult edit(@RequestBody Building building) {
        Long userId = getUserId();
        return toAjax(buildingService.editBuilding(building,userId));
    }

    /**
     * 删除楼栋信息
     */
    @PreAuthorize("@ss.hasPermi('house:building:remove')")
    @DeleteMapping("/remove/{buildingIds}")
    @TenantScope
    @RepeatSubmit
    public AjaxResult remove(@PathVariable String[] buildingIds) {
        return toAjax(buildingService.deleteBuildingByBuildingIds(buildingIds));
    }

    /**
     * 统计楼栋数据
     */
    @GetMapping(value = "/statistics")
    @TenantScope
    public AjaxResult getStatistics() {
        return success(buildingService.getStatistics());
    }

    /**
     * 下载楼栋模版
     */
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) {
        ExcelUtil<Building> util = new ExcelUtil<>(Building.class);
        util.importTemplateExcel(response, "楼栋模版");
    }

    /**
     * 导入楼栋信息
     */
    @PreAuthorize("@ss.hasPermi('house:building:importData')")
    @PostMapping("/importData")
    @TenantScope
    public AjaxResult importData(MultipartFile file) throws Exception {
        ExcelUtil<Building> util = new ExcelUtil<>(Building.class);
        List<Building> buildingList = util.importExcel(file.getInputStream());
        String username = getUsername();
        Long userId = getUserId();
        String message = buildingService.importUser(buildingList, username, userId);
        return success(message);
    }
}
